Structs
In , a record (also called a structure, , or compound data) is a basic . Records in a or are usually called "rows". , possibly of different data types, typically in fixed number and sequence.}} The fields of a record may also be called members, particularly in ; fields may also be called elements, though these risk confusion with the elements of a . by the fact that their number of fields is typically fixed, each field has a name, and that each field may have a different type.}} A record type is a that describes such values and variables. Most modern computer languages allow the programmer to define new record types. The definition includes specifying the data type of each field and an (name or label) by which it can be accessed. In , s (with no field names) are generally preferred due to their simplicity, but proper record types are studied in languages such as . Since type-theoretical records may contain -typed fields in addition to data, they can express many features of . Records can exist in any storage medium, including and such as s or s. Records are a fundamental component of most data structures, especially s. Many computer s are organized as arrays of s, often grouped into larger physical records or for efficiency. The parameters of a can often be viewed as the fields of a record variable; and the arguments passed to that function can be viewed as a record value that gets to that variable at the time of the call. Also, in the that is often used to implement procedure calls, each entry is an activation record or call frame, containing the procedure parameters and local variables, the return address, and other internal fields. An object in language is essentially a record that contains procedures specialized to handle that record; and object types are an elaboration of record types. Indeed, in most object-oriented languages, records are just special cases of objects, and are known as s (PODSs), to contrast with objects that use OO features. A record can be viewed as the computer analog of a , although a may or may not be considered a record, and vice versa, depending on conventions and the specific programming language. In the same vein, a record type can be viewed as the computer language analog of the of two or more , or the implementation of an abstract in a specific language. Keys A record may have zero or more key''s. A key is a field or set of fields in the record that serves as an identifier. A unique key is often called the ''primary key, or simply the record key. For example an employee file might contain employee number, name, department, and salary. The employee number will be unique in the organization and would be the primary key. Depending on the storage medium and file organization the employee number might be —that is also stored in a separate file to make lookup faster. The department code may not be unique; it may also be indexed, in which case it would be considered a secondary key, or alternate key. If it is not indexed the entire employee file would have to be scanned to produce a listing of all employees in a specific department. The salary field would not normally be considered usable as a key. Indexing is one factor considered when designing a file. History , showing tabular data with rows of data, each a record corresponding to a single person.}} The concept of record can be traced to various types of s and s used in since remote times. The modern notion of records in computer science, with fields of well-defined type and size, was already implicit in 19th century mechanical calculators, such as 's . (1895)}} The original machine-readable medium used for data (as opposed to control) was used for records in the : each punch card was a single record. Compare the journal entry from 1880 and the punch card from 1895. Records were well established in the first half of the 20th century, when most data processing was done using punched cards. Typically each record of a data file would be recorded in one punched card, with specific columns assigned to specific fields. Generally, a record was the smallest unit that could be read in from external storage (e.g. card reader, tape or disk). Most implementations and early s did not have special syntax for records, but the concept was available (and extensively used) through the use of s, , and . Some early computers, such as the , had hardware support for delimiting records and fields, and special instructions for copying such records. The concept of records and fields was central in some early file and utilities, such as . }} was the first widespread programming language to support record types, and its record definition facilities were quite sophisticated at the time. The language allows for the definition of nested records with alphanumeric, integer, and fractional fields of arbitrary size and precision, as well as fields that automatically format any value assigned to them (e.g., insertion of currency signs, decimal points, and digit group separators). Each file is associated with a record variable where data is read into or written from. COBOL also provides a MOVE CORRESPONDING statement that assigns corresponding fields of two records according to their names. The early languages developed for numeric computing, such as (up to ) and , did not have support for record types; but later versions of those languages, such as and did add them. The original too was lacking records (except for the built-in ), but its s provided an adequate surrogate. The was one of the first languages to fully integrate record types with other basic types into a logically consistent type system. The programming language provided for COBOL-style records. The programming language initially provided the record concept as a kind of template ( ) that could be laid on top of a memory area, rather than a true record data type. The latter were provided eventually (by the declaration), but the two concepts are still distinct in the language. Most languages designed after Pascal (such as , , and ) also supported records. Operations * Declaration of a new record type, including the position, type, and (possibly) name of each field; * Declaration of variables and values as having a given record type; * Construction of a record value from given field values and (sometimes) with given field names; * Selection of a field of a record with an explicit name; * Assignment of a record value to a record variable; * Comparison of two records for equality; * Computation of a standard for the record. The selection of a field from a record value yields a value. Some languages may provide facilities that enumerate all fields of a record, or at least the fields that are references. This facility is needed to implement certain services such as s, , and . It requires some degree of . In systems with record subtyping, operations on values of record type may also include: * Adding a new field to a record, setting the value of the new field. * Removing a field from a record. In such settings, a specific record type implies that a specific set of fields are present, but values of that type may contain additional fields. A record with fields x'', ''y, and z'' would thus belong to the type of records with fields ''x and y'', as would a record with fields ''x, y'', and ''r. The rationale is that passing an (x'',''y,z'') record to a function that expects an (''x,y'') record as argument should work, since that function will find all the fields it requires within the record. Many ways of practically implementing records in programming languages would have trouble with allowing such variability, but the matter is a central characteristic of record types in more theoretical contexts. Assignment and comparison Most languages allow assignment between records that have exactly the same record type (including same field types and names, in the same order). Depending on the language, however, two record data types defined separately may be regarded as distinct types even if they have exactly the same fields. Some languages may also allow assignment between records whose fields have different names, matching each field value with the corresponding field variable by their positions within the record; so that, for example, a with fields called real and imag can be assigned to a record variable with fields X and Y. In this alternative, the two operands are still required to have the same sequence of field types. Some languages may also require that corresponding types have the same size and encoding as well, so that the whole record can be assigned as an uninterpreted . Other languages may be more flexible in this regard, and require only that each value field can be legally assigned to the corresponding variable field; so that, for example, a field can be assigned to a field, or vice versa. Other languages (such as ) may match fields and values by their names, rather than positions. These same possibilities apply to the comparison of two record values for equality. Some languages may also allow order comparisons ('<'and '>'), using the based on the comparison of individual fields. allows both of the preceding types of assignment, and also allows ''structure expressions, such as a = a+1; where "a" is a record, or structure in PL/I terminology. Algol 68's distributive field selection In Algol 68, if Pts was an array of records, each with integer fields X and Y, one could write Pts.Y to obtain an array of integers, consisting of the Y fields of all the elements of Pts. As a result, the statements Pts3.Y := 7 and Pts.Y3 := 7 would have the same effect. Pascal's "with" statement In the , the command with R do S would execute the command sequence S as if all the fields of record R had been declared as variables. So, instead of writing Pt.X := 5; Pt.Y := Pt.X + 3 one could write with Pt do begin X := 5; Y := X + 3 end. Representation in memory The representation of records in memory varies depending on the programming languages. Usually the fields are stored in consecutive positions in memory, in the same order as they are declared in the record type. This may result in two or more fields stored into the same word of memory; indeed, this feature is often used in to access specific bits of a word. On the other hand, most compilers will add padding fields, mostly invisible to the programmer, in order to comply with alignment constraints imposed by the machine—say, that a field must occupy a single word. Some languages may implement a record as an array of addresses pointing to the fields (and, possibly, to their names and/or types). Objects in object-oriented languages are often implemented in rather complicated ways, especially in languages that allow . Self-defining records A self-defining record is a type of record which contains information to identify the record type and to locate information within the record. It may contain the offsets of elements; the elements can therefore be stored in any order or may be omitted. Alternatively, various elements of the record, each including an element identifier, can simply follow one another in any order. See also *Data structure References Category:Computer science